﻿using aspNetCURD.Database;
using aspNetCURD.Models;
using aspNetCURD.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace aspNetCURD.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class CourseController : ControllerBase
    {
        private readonly ICourseRepository _courseRepository;
        private readonly AppDbContext _context;
        public CourseController(ICourseRepository courseRepository, AppDbContext context)
        {
            _courseRepository = courseRepository;
            _context = context;
        }
        /// <summary>
        /// 获取相关课程信息
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetCourses([FromQuery] string keyword, int pageIndex, int pageSize)
        {
            var courseRepository = _courseRepository.GetCourses(keyword, pageIndex, pageSize);
            if(courseRepository == null)
            {
                return NotFound("未找到课程信息");
            }
            return Ok(courseRepository);
        }
        /// <summary>
        /// 获取相关课程信息条数
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns></returns>
        [HttpGet("CourseCount")]
        public IActionResult GetCourseCount([FromQuery] string keyword)
        {
            var courseRepository = _courseRepository.GetCourseCount(keyword);
            if (courseRepository == 0)
            {
                return NotFound("未找到课程信息");
            }
            return Ok(courseRepository);
        }
        /// <summary>
        /// 根据课程号获取课程信息
        /// </summary>
        /// <param name="sno"></param>
        /// <returns></returns>
        [HttpGet("{sno}")]
        public IActionResult GetCourse([FromRoute] string sno)
        {
            if (string.IsNullOrEmpty(sno))
            {
                return BadRequest("输入为空或请求错误");
            }
            var courseRepository = _courseRepository.GetCourse(sno);
            if (courseRepository == null)
            {
                return NotFound("未找到课程信息");
            }
            return Ok(courseRepository);
        }
        /// <summary>
        /// 新增课程信息
        /// </summary>
        /// <param name="course"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddCourse([FromBody] course course)
        {
            if (course == null)
            {
                return BadRequest("输入为空或请求错误");
            }
            if (course.classhour < 2 || course.classhour > 6)
            {
                return BadRequest("classhour的值为 2~6");
            }
            if (course.credit < 2 || course.credit > 5)
            {
                return BadRequest("credit的值为 2~5");
            }
            try
            {
                _courseRepository.AddCourse(course);
                _courseRepository.Sava();
            }
            catch
            {
                throw;
            }
            return Ok("新增课程信息成功");
        }
        /// <summary>
        /// 修改课程信息
        /// </summary>
        /// <param name="cno"></param>
        /// <param name="course"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdateCourse([FromQuery] string cno, [FromBody] course course)
        {
            if(course == null || string.IsNullOrEmpty(cno))
            {
                return BadRequest("输入为空或请求错误");
            }
            if(cno.Trim() != course.cno.Trim())
            {
                return BadRequest("输入为空或请求错误");
            }
            if(course.classhour < 2 || course.classhour > 6)
            {
                return BadRequest("classhour的值为 2~6");
            }
            if(course.credit <2 || course.credit > 5)
            {
                return BadRequest("credit的值为 2~5");
            }
            _context.Entry(course).State = EntityState.Modified;
            try
            {
                _context.SaveChanges();
            }
            catch
            {
                throw;
            }
            return Ok("修改课程信息成功");
        }
        /// <summary>
        /// 删除课程信息
        /// </summary>
        /// <param name="cno"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DeleteCourse([FromQuery] string cno)
        {
            if (string.IsNullOrEmpty(cno))
            {
                return BadRequest("输入为空或请求错误");
            }
            var courseRepository = _courseRepository.GetCourse(cno);
            if (courseRepository == null)
            {
                return BadRequest("输入为空或请求错误");
            }

            _context.Entry(courseRepository).State = EntityState.Deleted;
            try
            {
                _context.SaveChanges();
            }
            catch
            {
                throw;
            }
            return Ok("删除课程信息成功");
        }
    }
}
